import 'package:dynamic_stock/pages/carinfo_page.dart';
import 'package:dynamic_stock/pages/company_page.dart';
import 'package:dynamic_stock/pages/home_page.dart';
import 'package:flutter/material.dart';
import '../const/const.dart';

class RootPage extends StatefulWidget {
  @override
  _RootPageState createState() => _RootPageState();
}

class _RootPageState extends State<RootPage> {

  final PageController _controller = PageController(initialPage: 0);
  int _currentIndex = 0;

  BottomNavigationBarItem _createNaviItem(String title, String activeIcon, String inactiveIcon, int index) {
    return BottomNavigationBarItem(
      icon: Image.asset(inactiveIcon),
      title: Text(title, style: index == _currentIndex ? AppTextStyle.tabbarActiveStyle : AppTextStyle.tabbarNormalStyle),
      activeIcon: ColorFiltered(
        colorFilter: ColorFilter.mode(AppMainColor.mainColor, BlendMode.srcIn),
        child: Image.asset(activeIcon),
      )
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: PageView(
        controller: _controller,
        physics: NeverScrollableScrollPhysics(), // 禁止滑动
        onPageChanged: (index){
          setState(() {
            _currentIndex = index;
          });
        },
        children: <Widget>[
          HomePage(),
          CompanyPage(),
          CarinfoPage()
        ],
      ),
      bottomNavigationBar: BottomNavigationBar(
        selectedFontSize: 10.0,
        unselectedFontSize: 10.0,
        backgroundColor: Colors.white,
        currentIndex: _currentIndex,
        onTap: (idx){
          _controller.jumpToPage(idx);
          setState(() {
            _currentIndex = idx;
          });
        },
        items: [
          _createNaviItem("动态股份", "assets/tab_1_active.png", "assets/tab_1_inactive.png", 0),
          _createNaviItem("车管家公司", "assets/tab_2_active.png", "assets/tab_2_inactive.png", 1),
          _createNaviItem("车辆信息", "assets/tab_3_active.png", "assets/tab_3_inactive.png", 2),
        ],
      ),
    );
  }
}